<template>
  <el-container class="home">
    <el-header>
      <div class="title">
        <img src="../assets/heima.png" alt>
        <span>电商管理后台系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside width="toggle?'200px':'64px'">
        <div class="collapse" @click="toggle=!toggle">{{toggle?'→':'←'}}</div>
        <el-menu
          :unique-opened="true"
          background-color="#4a4f5c"
          text-color="#fff"
          active-text-color="#ffd04b"
          :collapse="toggle"
          :collapse-transition="false"
          :default-active="defActive"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menusList" :key="item.id">
            <template slot="title">
              <i class="icon" :class="iconClass[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="setDefActive('/'+subItem.path)">
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menusList: [],
      iconClass: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      toggle:false,
      defActive:''
    }
  },

  methods: {
    logout() {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 获取左侧菜单数据
    async getMenusList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status != 200) return this.$message.error('服务器异常')
      this.menusList = res.data
      this.$message.success(res.meta.msg)
    },
    setDefActive(path) {
      // console.log(path)
      sessionStorage.setItem('path',path)
      this.defActive = path
    }
  },

  created() {
    // 页面一加载就请求列表数据
    this.getMenusList()
    this.defActive = sessionStorage.getItem('path')
  }
}
</script>

<style lang='css' scoped>
.home {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  padding: 5px 10px;
  background: #688f92;
}
.title {
  display: flex;
  align-items: center;
  font-size: 25px;
  color: #fff;
}
.title img {
  margin-right: 10px;
}
.el-aside {
  background: #4a4f5c;
}
.icon {
  margin-right: 10px;
}
.el-menu {
  border-right: none;
}
.collapse {
  background: rgb(199, 147, 147);
  text-align: center;
  user-select: none;
  letter-spacing: 5px;
}
.el-main {
  background: #eee;
}
</style>
